import { Axios } from "@/axios/axiosPlugin";
import { baseURL } from "@/config";
 
class MyUploadAdapter {
    constructor(loader) {
        this.loader = loader;
    }
 
    // 启动上载过程
    async upload() {
        const data = new FormData();
        data.append("file", await this.loader.file);
    
        const res = await Axios({
          url: baseURL + `/essay/upload`,
          method: "POST",
          data,
        });
    
        console.log(res.data);
        // 后台返回数据：
        // {"code":0,"msg":"success","data":{"url":"/upload/struts2.jpeg"}}
    
        // 方法返回数据格式： {default: "url"}
        return {
          default: res.data.data
        };
      }
 
    // 中止上载过程
    abort() {
        if (this.xhr) {
            this.xhr.abort();
        }
    }
}
 
function MyCustomUploadAdapterPlugin(editor) {
    editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
        // 在这里将URL配置为后端上载脚本
        return new MyUploadAdapter(loader);
    };
}
 
export {
    MyUploadAdapter,
    MyCustomUploadAdapterPlugin
}